package com.hy.main.demo.rabbitmq;

import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

/**
 * @Author yang DongYong
 * @Date 2022/5/8 - 05 - 08 - 23:15
 * @Description: com.hy.main.demo.rabbitmq
 * @version: 1.0
 */
public class Worker01 {
    // 队列名称
    public static final String QUEUE_NAME = "Hello";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMqUtils.getChannel();
        // 匿名内部类方式接收消息 接收消息的回调
        DeliverCallback deliverCallback = (consumerTag, message) -> System.out.println("接收到的消息:" + new String(message.getBody()));
        // 取消消息的回调
        CancelCallback cancelCallback = consumerTag -> System.out.println("消息消费被中断:" + consumerTag);
        System.out.println("线程A1等待接收消息.....");
        channel.basicConsume(QUEUE_NAME, true, deliverCallback, cancelCallback);
    }
}

class Worker02 {
    // 队列名称
    public static final String QUEUE_NAME = "Hello";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMqUtils.getChannel();
        // 匿名内部类方式接收消息 接收消息的回调
        DeliverCallback deliverCallback = (consumerTag, message) -> System.out.println("接收到的消息:" + new String(message.getBody()));
        // 取消消息的回调
        CancelCallback cancelCallback = consumerTag -> System.out.println("消息消费被中断:" + consumerTag);
        System.out.println("线程A2等待接收消息.....");
        channel.basicConsume(QUEUE_NAME, true, deliverCallback, cancelCallback);
    }
}
